package com.luisliuyi.demo.optimize.network;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    NetUtil netUtil = new NetUtil();
    TextView tv_time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_time = findViewById(R.id.tv_time);
    }

    public void get(View view) {
        new Thread() {
            @Override
            public void run() {
                final long start = System.currentTimeMillis();
                for (int i = 0; i < 10; i++) {
                    HttpUrl httpUrl = new HttpUrl("http://www.kuaidi100.com/query?type=yuantong&postid=222222222");
                    Request request = new Request();
                    request.setHttpUrl(httpUrl);
                    String data = netUtil.call(request);
                    Log.i("liuyi", "data: "+data);
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv_time.setText("耗费时间: " +( System.currentTimeMillis() - start));
                    }
                });

            }
        }.start();
    }

    public void getMore(View view) {
        new Thread() {
            @Override
            public void run() {
                final long start = System.currentTimeMillis();
                for (int i = 0; i < 10; i++) {
                    String data= get("http://www.kuaidi100.com/query?type=yuantong&postid=222222222");
                    Log.i("liuyi", "data: "+data);
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv_time.setText("耗费时间: " +( System.currentTimeMillis() - start));
                    }
                });

            }
        }.start();
    }



    public String get(String requestUrl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;
        try {
            URL url = new URL(requestUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(60000);
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.connect();
            Map<String, List<String>> map = connection.getHeaderFields();
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                StringBuffer sbf = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null) {
                    sbf.append(line);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != br) {
                    br.close();
                }
                if (null != is) {
                    is.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            connection.disconnect();
        }
        return result;
    }

}
